package com.yyq.sos.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.SingleTableEntityPersister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yyq.servlets.ApplicationContextHandle;
import com.yyq.sos.dao.SysSequencesDao;
import com.yyq.sos.pojo.SysSequences;

public class SequencesUtil 
{
	private static final Map map = new HashMap(); 
	private final static SequencesUtil _instance = new SequencesUtil();
	private SysSequencesDao sysSequencesDao;
	
	private SequencesUtil(){
		sysSequencesDao = (SysSequencesDao)ApplicationContextHandle.getBean("sysSequencesDao");
	}
	
	private void seqs(){
		List list = sysSequencesDao.list(SysSequences.class);
		for(int i=0; i<list.size(); ++i){
			SysSequences obj = (SysSequences)list.get(i);
			Long[] value = new Long[3];
			value[0] = obj.getCurrValue(); //��ǰ����ֵ
			value[1] = obj.getIncrementValue(); //ÿ��ȡֵ�ļ��ֵ
			value[2] = new Long(1);         //�Ѿ�ʹ�õ�����ֵ����,��ֵ��������ֵ
			map.put(obj.getTableName(), value);
		}
	}
	public static void main(String[] ss)
	{
//		List list = new ArrayList();
//		for(int i=300; i<600;++i){
//			SysUsersRoles o = new SysUsersRoles();
//			o.setUserId(new Long(i));
//			o.setRoleId(new Long(i));
//			o.setGrantor(new Long(i));
//			o.setIsGrant("0");
//			o.setGrantDate(new Date());
//			list.add(o);
//		}
//		BpoFactory.getBpo().save(list.toArray());
		SequencesUtil s= SequencesUtil.getInstance();
		for(int i=0;i<300;++i)
			System.out.println("-------------  "+s.getNextPk("ddddddddddddddd"));
		
	}
	
	public static SequencesUtil getInstance(){
		return _instance;
	}
	
	synchronized public Long getNextPk(Class cls){
		Long currValue = null;
		String tableName = this.getTableName(cls);
		currValue = getNextPk(tableName);
		return currValue;
	}
	synchronized public Long getNextPk(String tableName){
		Long currValue = null;
		if(tableName==null || tableName.trim().equals(""))
			return currValue;
		try{
			tableName = tableName.toUpperCase();
			Long[] value = (Long[])map.get(tableName);
			if(value!=null && value[2].longValue()< value[1].longValue()){
				currValue = new Long(value[0].longValue());
				value[0] = new Long(value[0].longValue()+1);
				value[2] = new Long(value[2].longValue()+1);
			}else{
				SysSequences obj = sysSequencesDao.getSeqs(tableName);
				currValue = new Long(obj.getCurrValue().longValue());
				value = new Long[3];
				value[0] = new Long(obj.getCurrValue().longValue()+1);//currValue;//obj.getCurrValue();
				value[1] = obj.getIncrementValue();
				value[2] = new Long(1);
			}
			map.put(tableName, value);
		}catch(Exception e){
			e.printStackTrace();
		}
		return currValue;
	}
	
	private String getTableName(Class cls){
		SingleTableEntityPersister entityPersister=(SingleTableEntityPersister)sysSequencesDao.getSessionFactory().getClassMetadata(cls);
		return entityPersister.getTableName();
	}
}
